package com.cs.archivesmanagement.daoimpl;

import com.cs.archivesmanagement.dao.EntityDao;
import com.cs.archivesmanagement.entity.AmArchives;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by amo on 2015/8/20.
 */


@Repository
public class EntityDaoImpl extends  SuperDao implements EntityDao {
    @Override
    public Serializable addEntity(Object entity) {
        Serializable serializable=null;
        try {
            Session session=sessionFactory.openSession();
            Transaction tx= session.beginTransaction();
            serializable=session.save(entity);
            tx.commit();
            session.close();
        } catch (DataAccessException e) {
            //  logger.info(e.toString());
        }

        return  serializable;
    }

    @Override
    public boolean updateEntity(Object entity) {
        boolean isSuccess=false;

        try {
            System.out.println("u");
            Session session=sessionFactory.openSession();
            Transaction tx= session.beginTransaction();
            System.out.println("up");
            session.merge(entity);
            System.out.println("upp");
            tx.commit();
            session.close();
            isSuccess=true;
        } catch (Exception e) {
            //  logger.info(e.toString());
            System.out.println(e);
        }

        return  isSuccess;
    }

    @Override
    public boolean deleteEntity(Object entity) {
        boolean isSuccess=false;

        try {
            Session session=sessionFactory.openSession();
            Transaction tx= session.beginTransaction();
            session.delete(entity);
            tx.commit();
            session.close();
            isSuccess=true;
        } catch (DataAccessException e) {
            //  logger.info(e.toString());
        }

        return  isSuccess;
    }

    @Override
    public Object getEntity(Class aclass, Object entity) {
        Object objectEntity = null;
        try {
            Session session=sessionFactory.openSession();
            objectEntity = session.load(aclass, (Serializable) entity);
        } catch (DataAccessException e) {

        }
        return objectEntity;
    }


    @Override
    public List getEntityList(Class aclass, ArrayList<Criterion> criterionArrayList) {
               java.util.List list=new ArrayList<>();
        try {
            Session session=sessionFactory.openSession();
            Transaction tx= session.beginTransaction();


            if(criterionArrayList==null||criterionArrayList.size()==0){
                list = session.createCriteria(aclass).list();
            }

            else {

                Criteria criteria=session.createCriteria(aclass);

                for(Criterion criterion :criterionArrayList){
                    criteria.add(criterion);
                }
                list =criteria.list();

                System.out.print(list.size());
            }

            tx.commit();
            session.close();
        } catch (DataAccessException e) {
            //  logger.info(e.toString());
        }

        return list;
    }


}
